<%@page import="javax.faces.context.FacesContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<jsp:include page="header.jsp" />
<f:view>

	<h:form id="subscription_edit_form">
		<h:panelGroup layout="block" styleClass="t">
			<h:panelGroup layout="block" styleClass="b">
				<h:panelGroup layout="block" styleClass="l">
					<h:panelGroup layout="block" styleClass="r">
						<h:panelGroup layout="block" styleClass="bl">
							<h:panelGroup layout="block" styleClass="br">
								<h:panelGroup layout="block" styleClass="tl">
									<h:panelGroup layout="block" styleClass="tr">

										<h:panelGrid columns="1" cellspacing="0" cellpadding="5"
											border="0" styleClass="formtable" captionClass="mainTitle">
											<f:facet name="caption">
												<h:panelGroup>
													<h:panelGroup layout="block">
														<h:outputText value="Create subscription"
															rendered="#{subscriptionEditBean.subscriptionId == 0}"></h:outputText>
														<h:outputText value="Edit subscription"
															rendered="#{subscriptionEditBean.subscriptionId > 0}"></h:outputText>
													</h:panelGroup>
												</h:panelGroup>
											</f:facet>
											<h:panelGroup>
												<b><h:outputText value="Start date:(dd-MM-yyyy)"></h:outputText> </b>
												<h:message for="subscriptionStartDate" showSummary="true"
													showDetail="false" errorClass="errorMsg" />
												<br />
												<h:inputText id="subscriptionStartDate"
													value="#{subscriptionEditBean.subscription.startDate}" required="true"
													requiredMessage="Start date is required" label="Start date"
													styleClass="coolinp">
													<f:convertDateTime pattern="dd-MM-yyyy" type="date"/>
												</h:inputText>
											</h:panelGroup>
											<h:panelGroup>
												<b><h:outputText value="Duration (in months):"></h:outputText> </b>
												<h:message for="subscriptionDuration" showSummary="true"
													showDetail="false" errorClass="errorMsg" />
												<br />
												<h:selectOneListbox id="subscriptionDuration"
													value="#{subscriptionEditBean.subscription.duration}" required="true"
													requiredMessage="Duration is required" label="Duration" size="1"
													styleClass="coolinp">
													<f:selectItem itemLabel="1" itemValue="1"/>
													<f:selectItem itemLabel="4" itemValue="4"/>
													<f:selectItem itemLabel="6" itemValue="6"/>
													<f:selectItem itemLabel="12" itemValue="12"/>
												</h:selectOneListbox>
											</h:panelGroup>


											<h:panelGroup>
												<b><h:outputText value="Magazine:"></h:outputText></b>
												<h:message for="subscriptionMagazine" showSummary="true"
													showDetail="false" errorClass="errorMsg" />
												<br/>
													<h:selectOneListbox id="subscriptionMagazine" size="1" valueChangeListener="#{subscriptionEditBean.processValueChange }"
												value="#{subscriptionEditBean.magazineId}" label="Magazine" onchange="displayMagazineSuppliers();"
												styleClass="coolinp" required="true">
												<f:selectItems value="#{magazineBean.magazineItems}" />
											</h:selectOneListbox>
											</h:panelGroup>
											
											<h:panelGroup>
												<b><h:outputText value="Supplier:"></h:outputText></b>
												<h:message for="subscriptionSupplier" showSummary="true"
													showDetail="false" errorClass="errorMsg" />
												<br/>
													<h:selectOneListbox id="subscriptionSupplier" binding="#{subscriptionEditBean.supplierChooserComponent}" immediate="true" size="1"
												value="#{subscriptionEditBean.supplierId}" label="Supplier"
												styleClass="coolinp" required="true">
												<f:selectItems value="#{supplierBean.supplierItems}" />
											</h:selectOneListbox>
											</h:panelGroup>

											

											<h:panelGroup>												
												<input type="hidden" value="<%= request.getParameter("subscriptionId")!=null ? Integer.parseInt(request.getParameter("subscriptionId")) : 0 %>" name="subscriptionId"/>
												<input type="hidden" value="<%= request.getParameter("customerId")!=null ? Integer.parseInt(request.getParameter("customerId")) : 0 %>" name="customerId"/>
												
												<h:commandButton action="#{subscriptionEditBean.saveSubscription}"
													value="Save" accesskey="s" styleClass="frmbutton">
												</h:commandButton>
												<h:outputText value="&nbsp;" escape="false"></h:outputText>
												<h:commandButton action="#{subscriptionEditBean.cancel}"
													value="Cancel" accesskey="c" immediate="true"
													styleClass="frmbutton">
												</h:commandButton>
												<h:outputText value="&nbsp;" escape="false"></h:outputText>
												<h:commandButton action="#{subscriptionEditBean.deleteSubscription}"
													value="Delete" accesskey="d" immediate="true"
													styleClass="frmbutton"
													rendered="#{subscriptionEditBean.subscriptionId > 0}" onclick="if(!confirm('Are you sure you want to delete this subscription?'))return false;">
												</h:commandButton>
											</h:panelGroup>
										</h:panelGrid>
										<h:outputText escape="false" value="#{subscriptionEditBean.initMagazineSuppliers}"></h:outputText>

									</h:panelGroup>
								</h:panelGroup>
							</h:panelGroup>
						</h:panelGroup>
					</h:panelGroup>
				</h:panelGroup>
			</h:panelGroup>
		</h:panelGroup>
	</h:form>
</f:view>

<jsp:include page="footer.jsp" />
